#include "SockMessage.h"
#include <assert.h>

SockMessage::SockMessage()
{
	m_ReadPtr =0;
}
void SockMessage::Set(char c)
{

}
unsigned int SockMessage::GetLength()
{
	return m_Bytes.size();
}
const unsigned char* SockMessage::GetData() const
{
	return &m_Bytes[0];
}

void SockMessage::CreateTestData()
{
	WriteString("Rob Smells like a hobo");
}

void SockMessage::WriteInt(int i)
{
	int j =0;
	SDLNet_Write32(i,&j);
	m_Bytes.insert(m_Bytes.end(),(unsigned char*)&j,(unsigned char*)&j+sizeof(int));
}
void SockMessage::WriteFloat(float f)
{
	WriteInt(*(int*)&f);
}
void SockMessage::WriteString(std::string s)
{
	WriteInt(s.size());
	m_Bytes.insert(m_Bytes.end(),s.begin(),s.end());
}
bool SockMessage::ReadInt(int* i)
{
	assert(m_ReadPtr+sizeof(int) <= m_Bytes.size());
	*i = SDLNet_Read32(&m_Bytes[m_ReadPtr]);
	m_ReadPtr += sizeof(int);
	
	return true;
}
bool SockMessage::ReadFloat(float* f)
{
	int i;
	if(!ReadInt(&i))
	{
		return false;
	}
	*f = *((float*)&i);
	return true;
}
bool SockMessage::ReadString(std::string* s)
{
	int len = 0;
	if(!ReadInt(&len))
	{
		return false;
	}
	for (int i = 0; i < len; i++)
	{
		s->push_back(m_Bytes[m_ReadPtr + i]);
	}
//	s->insert(s->end(),&m_Bytes[m_ReadPtr],&m_Bytes[m_ReadPtr]+len);
	m_ReadPtr+=len;
	return true;
}
bool SockMessage::IsEmpty()
{
	if(m_Bytes.empty())
	{
		return true;
	}
	return false;
}